package com.liu.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduService.client.VodClient;
import com.liu.eduService.entity.EduVideo;
import com.liu.eduService.mapper.EduVideoMapper;
import com.liu.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.serviceBase.exception.MyException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2022-01-07
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public boolean getCountByChapterId(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        Integer count = baseMapper.selectCount(wrapper);
        return null != count && count > 0;
//        count 不为空或并且大于0  返回真
    }

    @Override
    public boolean deletevideoBycourseId(String courseId) {
        // 获取到 该课程id的所有视频id：
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        videoWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(videoWrapper);
        List<String> videoIdList = new ArrayList<>();

        for (int i = 0; i < videoList.size(); i++) {
            EduVideo eduVideo = videoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if (ObjectUtils.isEmpty(videoSourceId)){
                System.out.println("删除课程的所有视频，videoSourceId"+videoSourceId+"为空");
            }else {
                videoIdList.add(videoSourceId);
            }
        }
        vodClient.DeleteseveralVideo(videoIdList);
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(wrapper);
        return delete>0;


    }
}
